#!/bin/bash

cd /home/jinglun/h1/trident
CD_ERROR=$?
if [ $CD_ERROR != "0" ]
then
    echo "cannot change to dir /home/jinglun/h1/trident"
    exit $CD_ERROR
fi

ntpdate 172.16.55.254 >/dev/null
#make tmp
export VERSION_OLD=`cat version` 
export VERSION=`expr $VERSION_OLD + 1`
if [ -d ./filesystem ]
then
	echo   VERSION:$VERSION
else
	mkdir filesystem
	echo   VERSION:$VERSION  
fi


#clean old filesystem
cd filesystem
CD_ERROR=$?
if [ $CD_ERROR != "0" ]
then
    echo "cannot change to dir /home/jinglun/h1/trident/filesystem"
    exit $CD_ERROR
fi
rm -rf *
cd ..

#use routon_files
echo "Copy files from routon_files"
cp routon_files/* -a filesystem/

#usr libs
echo "Copy usr library from build/lib"
cp -a build/lib filesystem/hdisk/usr/

#update trident sdk files
echo "Update Trident Files in build/td_lib"
if [ -f script/trident_sdkfs_install.sh ]
    then ./script/trident_sdkfs_install.sh
fi
#use td_libs
echo "Copy trident library from build/td_lib"
#yes n|cp -ir build/td_lib/* filesystem/lib
cp -a build/td_lib/* filesystem/hdisk/lib

echo "Copy base library from compiler"
cp -a build/compiler/lib/*.so* ./filesystem/hdisk/lib



#mv OpenGLES libs
mv filesystem/lib/libEGL* filesystem/hdisk/usr/lib
mv filesystem/lib/libGLES* filesystem/hdisk/usr/lib
mv filesystem/lib/libIMG* filesystem/hdisk/usr/lib
mv filesystem/lib/libOpenVG* filesystem/hdisk/usr/lib

#rm files in guisys.tar.gz
rm -rf filesystem/usr/lib/libclutter*
rm -rf filesystem/usr/lib/libmx*
rm -rf filesystem/usr/lib/libng-toolkit*
rm -rf filesystem/usr/lib/libengine*
rm -rf filesystem/usr/lib/libGL.so.1.2
rm -rf filesystem/usr/lib/libGL.so.1
ln -sf /usr/lib/libdogless.so filesystem/usr/lib/libGL.so.1

#wget other modules from ftpserver
cd filesystem/hdisk
FTP_SERVER=ftp://172.16.55.91/pub/release
wget -r -nd -np -A 'H1_IM*.STD.*.tar.gz' $FTP_SERVER -O im.tar.gz
wget -r -nd -np -A 'H1_IPTV*.STD.*.tar.gz' $FTP_SERVER -O iptv.tar.gz
wget -r -nd -np -A 'H1_CHROME*.STD.*.tar.gz' $FTP_SERVER -O chrome.tar.gz
wget -r -nd -np -A 'H1_WEATHERWIDGET*.STD.*.tar.gz' $FTP_SERVER -O weather.tar.gz
wget -r -nd -np -A 'H1_GUISYS*.STD.*.tar.gz' $FTP_SERVER -O guisys.tar.gz

tar xf im.tar.gz
tar xf iptv.tar.gz
tar xf chrome.tar.gz
tar xf weather.tar.gz
tar xf guisys.tar.gz

rm -f im.tar.gz
rm -f iptv.tar.gz
rm -f chrome.tar.gz
rm -f weather.tar.gz
rm -f guisys.tar.gz

#add apps files
if [ -f /home/jinglun/h1/trident/script/apps_tarball_gen.sh ]
then 
    /home/jinglun/h1/trident/script/apps_tarball_gen.sh
fi

#change dir to filesystem
cd ..

#rm .svn
echo "Delete .svn dir"
rm -rf `find ./ -name "\.svn"`

#chmod file
echo "Chmod 755 all files"
chmod 755  -R *

#.bashrc
echo "Copy .bashrc"
cp -a ./home/.bashrc ./


#make link sbin 
ln -sf bin sbin


#strip files 
echo "Strip files"
../build/compiler/bin/arm-linux-strip `file $(find .)|grep "not stripped"|awk -F : '{print $1}'|grep -v "\.ko$"`

#make package 
echo "Make package"
tar zcf ../recovery/rec_filesystem/filesystem.tar.gz *


#test changes
